<?php

/**
 * This is the model class for table "player".
 *
 * The followings are the available columns in table 'player':
 * @property integer $id
 * @property string $name
 * @property string $created
 * @property string $updated
 * @property string $alliance_short
 * @property string $alliance_long
 * @property string $meta_short
 * @property string $meta_long
 *
 * The followings are the available model relations:
 * @property Planet[] $planets
 * @property Research[] $researches
 */
class Player extends CActiveRecord
{
  public $ally = '';
  public $meta = '';

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Player the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'player';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('name', 'length', 'max'=>30),
			array('alliance_short, meta_short', 'length', 'max'=>15),
      array('alliance_long, meta_long', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, last_update, alliance_short, alliance_long, meta_short, meta_long, ally, meta', 'safe', 'on'=>'search'),
      array('created, updated', 'safe'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'planets' => array(self::HAS_MANY, 'Planet', 'player_id'),
			'researches' => array(self::HAS_MANY, 'Research', 'player_id'),
		);
	}

  public function behaviors()
  {
    return array(
      'CTimestampBehavior' => array(
        'class' => 'zii.behaviors.CTimestampBehavior',
        'createAttribute' => 'created',
        'updateAttribute' => 'updated',
      )
    );
  }

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'created' => 'Created',
			'updated' => 'Updated',
			'alliance_short' => 'Ally Short',
			'alliance_long' => 'Ally',
			'meta_short' => 'Meta Short',
			'meta_long' => 'Meta Long',
      'ally' => 'Ally',
      'meta' => 'Meta'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('updated',$this->updated,true);
		$criteria->compare('alliance_short',$this->alliance_short,true);
		$criteria->compare('alliance_long',$this->alliance_long,true);
		$criteria->compare('meta_short',$this->meta_short,true);
		$criteria->compare('meta_long',$this->meta_long,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

  public function getAlly($data)
  {
    if (!empty($data->alliance_long))
      return $data->alliance_long.' ['.$data->alliance_short.']';
    else
      return NULL;
  }

  public function getMeta($data)
  {
    if (!empty($data->meta_long))
      return $data->meta_long.' ['.$data->meta_short.']';
    else
      return NULL;
  }

  public function loadPlayerByName($name)
  {
    $model = Player::model()->findByAttributes(array('name'=>$name));
    if ($model === null) {
      Yii::log('Player '.$name.' NICHT gefunden', CLogger::LEVEL_TRACE, 'sntools.controller.parser');
      return NULL;
    } else {
      Yii::log('Player '.$name.' gefunden', CLogger::LEVEL_TRACE, 'sntools.controller.parser');
    }

    return $model;
  }
}